package net.liteheaven.xblib.plugin;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Environment;

import java.io.File;

import dalvik.system.DexClassLoader;

public class PathUtil {

    static Context ctx(){
        return PluginKit.getInstance().getContext();
    }

    static String getDexOutDir(Context context){
        return context.getDir("optimizedDirectory", Context.MODE_PRIVATE).getAbsolutePath();
    }

    static String getLibOutDir(Context context){
        return context.getDir("librarySearchPath", Context.MODE_PRIVATE).getAbsolutePath();
    }

    static String getInnerApkDir(Context context){
        File innerDirFile = context.getCacheDir();
        return innerDirFile.getPath();
    }

    public static String getExternalApkDir(){
        File file = Environment.getExternalStorageDirectory();
        return file.getPath();
    }

    static String getExternalApkPath(String pluginName){
        String dir = getExternalApkDir();
        return dir + File.separator + pluginName + ".apk";
    }

    static String getInnerApkPath(String pluginName){
        String dir = getInnerApkDir(ctx());
        return dir + "/" + pluginName + ".apk";
    }

    static DexClassLoader getClassLoader(String apkPath){
        Context context = ctx();
        return new DexClassLoader(apkPath,
                getDexOutDir(context),
                getLibOutDir(context),
                context.getClassLoader());
    }
}
